import HTTP_STATUS_CODE from 'http-status-codes'
import constant from '../constant/index'
import {message} from 'antd'
import * as request from '../constant/fetchRequest'
import {convertContent} from "../constant/lang-util";
import {getNotificationUnRead} from "./notification";

const refreshUserInfo = user => ({
  type: 'REFRESH_USER_INFO',
  user
})

const refreshSocialInfo = data => ({
  type: 'GET_SOCIAL_INFO',
  data
})
export const modifyNodeBBEditStatus = status => ({
  type: 'MODIFY_NODEBB_EDIT_STATUS',
  status
})
export const refreshMyOrganizations = data => ({
  type: 'REFRESH_ORGANIZATIONS',
  data
})

export const modifyEditPasswordStatus = status => ({
  type: 'MODIFY_EDIT_PASSWORD_STATUS',
  status
})

export const loadAppsAction = (apps) => ({
  type: 'LOAD_APPS',
  apps
})

export const refreshEmailResetResult = (data) => ({
  type: 'RESET_EMAIL',
  ...data
});


export const getUserInfo = () => {
  return dispatch => {
    (async () => {
      const res = await request.get(`./api/users/21`)
        if (res.status === HTTP_STATUS_CODE.OK) {
            dispatch(getNotificationUnRead())
            dispatch(getSocialInfo(res.body.userName))
            dispatch(refreshUserInfo(res.body))
            dispatch(getMyOrganizations(res.body.id))
        }
    })()
  }
}

export const getMyOrganizations = userId => {
  return dispatch => {
    (async () => {
      const res = await request.get(`./api/users/${userId}/organizations`)
        if (res.status === HTTP_STATUS_CODE.OK) {
            dispatch(refreshMyOrganizations(res.body))
        }
    })()
  }
}

export const createNewNodeBBUser = () => {
  return dispatch => {
    (async () => {
      const res = await request.post(`./api/users/1/nodeBBUser`)
      if (res.status === HTTP_STATUS_CODE.CREATED) {
        const {username: nodeBBUsername, password: nodeBBPassword} = res.body
        dispatch(refreshUserInfo({nodeBBUsername, nodeBBPassword}))
        dispatch(modifyNodeBBEditStatus(constant.NODEBB_EDIT_STATUS.SHOW))
        message.success(convertContent('nodeBB 账号创建成功！为保证论坛和系统账号同步！请重新登录'), 2, () => {
          window.location.href = "https://school.thoughtworks.cn/learn/logout/cas"
        })
      }
    })()
  }
}

export const updateNodeBBUser = nodeBBUser => {
  return dispatch => {
    (async () => {
      const res = await request.update(`./api/users/1/nodeBBUser`, nodeBBUser)
      if (res.status === HTTP_STATUS_CODE.NO_CONTENT) {
        const { username: nodeBBUsername } = nodeBBUser
        dispatch(refreshUserInfo({nodeBBUsername, nodeBBPassword: '******'}))
        dispatch(modifyNodeBBEditStatus(constant.NODEBB_EDIT_STATUS.SHOW))
        message.success(convertContent('论坛 账号修改成功！！为保证论坛和系统账号同步！请重新登录'), 2, () => {
          window.location.href = "https://school.thoughtworks.cn/learn/logout/cas"
        })
      }
    })()
  }
}

export const getSocialInfo = (username) => {
    return dispatch => {
        (async () => {
            const res = await request.get(`./api/users/social/account?username=${username}`)
            if (res.status === HTTP_STATUS_CODE.OK) {
                dispatch(refreshSocialInfo(res.body))
            }
            else {
                message.error(res.body.message)
            }
        })()
    }
}
export const submitPasswords = passwords => {
  return dispatch => {
    (async () => {
      const res = await request.update(`./api/users/1/password`, passwords)
      if (res.status === HTTP_STATUS_CODE.NO_CONTENT) {
        dispatch(modifyEditPasswordStatus(constant.PASSWORD_EDIT_STATUS.SHOW))
        message.success(convertContent('密码修改成功'))
      }
    })()
  }
}


export const unBinding = (username) => {
    return dispatch => {
        (async () => {
            const res = await request.del(`./api/users/social/unbinding?username=${username}`)
            if (res.status === HTTP_STATUS_CODE.NO_CONTENT) {
                dispatch(getSocialInfo(username))
                message.success(convertContent('解绑成功'))
            }
        })()
    }
}


export const submitUserDetail = userDetail => {
  return dispatch => {
    (async () => {
      const res = await request.update(`./api/users/1/detail`, userDetail)
      if (res.status === HTTP_STATUS_CODE.NO_CONTENT) {
        dispatch(refreshUserInfo(userDetail))
        message.success(convertContent('用户详情修改成功'))
      }
    })()
  }
}

export const loadApps = () => {
  return (dispatch) => {
    (async () => {
      const res = await request.get(`./api/users/1/apps`)
      if (res.status === HTTP_STATUS_CODE.OK) {
        dispatch(loadAppsAction(res.body))
      }
    })()
  }
}

export const resetEmail = (uuid) => {
  return (dispatch) => {
    (async () => {
      const res = await request.update(`./api/email-reset/${uuid}`)
      if (res && res.status === HTTP_STATUS_CODE.OK) {
        return dispatch(refreshEmailResetResult(res.body))
      }
    })()
  }
}

export const getEmailResetResult = () => {
  return dispatch => {
    (async () => {
      const res = await request.get(`./api/email-reset/users/1`)
      return dispatch(refreshEmailResetResult(res.body));
    })()
  }
}

export const createResetEmail = (data) => {
  return dispatch => {
    (async () => {
      const res = await request.post('./api/email-reset/users/1', data)
      if (res && res.status === HTTP_STATUS_CODE.CREATED) {
        return dispatch(refreshEmailResetResult(res.body));
      }
    })()
  }
}


export const updateUserCurrentOrganizationId = (data) => {
  return dispatch => {
    (async () => {
      const res = await request.update(`../program-center/api/users/${data.userId}/organizations`, data)
      if (res.status === HTTP_STATUS_CODE.NO_CONTENT) {
        dispatch(getUserInfo())
        dispatch(loadApps())
      }
    })()
  }
}